package org.gameg.Api.entity;

import org.gameg.model.biz.SysUser;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by Administrator on 2017/7/31.
 */
public class SysUserOperate {
    public static final String LOGIN_USER = "loginUser";

    public static void setUser(SysUser user, HttpSession session) {
        session.setAttribute(LOGIN_USER, user);
    }

    public static SysUser getUser(HttpSession session) {
        return (SysUser) session.getAttribute(LOGIN_USER);
    }

    public static SysUser getUser() {
        return (SysUser) getsession().getAttribute(LOGIN_USER);
    }

    public static HttpSession getsession() {
        return getRequest().getSession();
    }

    public static void removeUserSession(){
        getsession().removeAttribute(LOGIN_USER);
    }

    public static HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        return request;
    }

    public static void logout(){
        getsession().invalidate();
    }


}
